import os
import smtplib
from datetime import datetime
from email.mime.text import MIMEText
from utils.read_ini import read_server

class EmailManage:

    def send_email(self, test_results_total, test_results, pass_rate):
        # body = f"""\
        # <html>
        #   <head><title>自动化执行统计</title></head>
        #   <body>
        #     <p>总执测试用例：{test_results_total}条</p>
        #     <p>通过测试用例：{test_results['passed']}条</p>
        #     <p>失败测试用例：{test_results['failed']}条</p>
        #     <p>跳过测试用例：{test_results['skipped']}条</p>
        #     <p>测试通过率：{pass_rate:.2f}%</p>
        #   </body>
        # </html>
        # """
        body = f"""\
        <html>
          <head>
            <meta charset="utf-8">
            <title>自动化测试报告</title>
            <style>
              body {{ font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6; }}
              .report-title {{ 
                color: #2c3e50; 
                border-bottom: 3px solid #3498db;
                padding-bottom: 10px;
              }}
              .stats-table {{
                width: 100%;
                max-width: 600px;
                border-collapse: collapse;
                margin: 20px 0;
                box-shadow: 0 1px 3px rgba(0,0,0,0.12);
              }}
              .stats-table td, .stats-table th {{
                padding: 12px;
                text-align: left;
                border-bottom: 1px solid #ecf0f1;
              }}
              .stats-table tr:hover {{ background-color: #f9f9f9; }}
              .progress-bar {{
                height: 20px;
                background-color: #eee;
                border-radius: 10px;
                overflow: hidden;
              }}
              .progress-fill {{
                height: 100%;
                width: {pass_rate}%;
                background: linear-gradient(90deg, #2ecc71, #27ae60);
                transition: width 0.5s ease;
              }}
              .failed {{ color: #e74c3c; font-weight: bold; }}
              .skipped {{ color: #f1c40f; }}
            </style>
          </head>
          <body>
            <h1 class="report-title">自动化测试执行报告</h1>

            <table class="stats-table">
              <tr>
                <th colspan="2" style="background-color: #f8f9fa;">核心指标概览</th>
              </tr>
              <tr>
                <td>总用例数</td>
                <td>{test_results_total}</td>
              </tr>
              <tr>
                <td>通过用例</td>
                <td>{test_results['passed']} <span style="color: #27ae60;">✓</span></td>
              </tr>
              <tr>
                <td class="failed">失败用例</td>
                <td class="failed">{test_results['failed']} <span>✗</span></td>
              </tr>
              <tr>
                <td class="skipped">跳过用例</td>
                <td class="skipped">{test_results['skipped']} <span>⚠</span></td>
              </tr>
              <tr>
                <td>通过率</td>
                <td>
                  {pass_rate:.2f}%
                  <div class="progress-bar">
                    <div class="progress-fill"></div>
                  </div>
                </td>
              </tr>
            </table>

            <h3>统计摘要：</h3>
            <ul>
              <li>失败率：{(test_results['failed'] / test_results_total * 100):.2f}%</li>
              <li>执行完成率：{((test_results['passed'] + test_results['failed']) / test_results_total * 100):.2f}%</li>
              <li>跳过占比：{(test_results['skipped'] / test_results_total * 100):.2f}%</li>
            </ul>

            <div style="color: #7f8c8d; font-size: 0.9em; margin-top: 30px;">
              <hr>
              <p>报告生成时间：{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</p>
              <p>执行环境：{os.environ.get('ENV', 'Test')} 环境</p>
            </div>
          </body>
        </html>
        """
        # 转换成浏览器可以识别的模式
        msg = MIMEText(body, 'html')
        msg['subject'] = read_server("email", 'subject')

        # 发送邮件
        msg['from'] = read_server("email", 'username')
        # 接受邮箱
        msg['to'] = read_server("email", 'to_username')

        # 奖励邮箱链接465
        em = smtplib.SMTP_SSL(read_server("email", 'smtp_sever'))
        # 邮件和邮件的鉴权码
        em.login(read_server("email", 'username'), read_server("email", 'password'))
        # # 邮件发送
        em.sendmail(read_server("email", 'username'), read_server("email", 'to_username'), msg.as_string())
        em.quit()

